//
//  UIImage+SomeMethods.m
//  BKJ
//
//  Created by 吴涛 on 16/2/29.
//  Copyright © 2016年 贝壳家iOS. All rights reserved.
//

#import "UIImage+SomeMethods.h"

@implementation UIImage (SomeMethods)
- (UIImage *)dw_drawRectWithRoundedCorner:(CGFloat)radius Size:(CGSize)sizetoFit{
  
  CGRect frame = CGRectMake(0, 0, sizetoFit.width, sizetoFit.height);

  UIGraphicsBeginImageContextWithOptions(frame.size, NO, [UIScreen mainScreen].scale);
  
  UIBezierPath *path  = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
  
  CGContextAddPath(UIGraphicsGetCurrentContext(), path.CGPath);
  
  CGContextClip(UIGraphicsGetCurrentContext());
  
  [self drawInRect:frame];
  
  CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
  
  UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
  
  UIGraphicsEndImageContext();
  
  return output;
  
}

- (UIImage *)dw_radiusImage{
  // 开始图形上下文
  UIGraphicsBeginImageContextWithOptions(self.size, NO, [UIScreen mainScreen].scale);
  
  // 获得图形上下文
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  
  // 设置一个范围
  CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
  
  // 根据一个rect创建一个椭圆
  CGContextAddEllipseInRect(ctx, rect);
  
  // 裁剪
  CGContextClip(ctx);
  
  // 将原照片画到图形上下文
  [self drawInRect:rect];
  
  // 从上下文上获取剪裁后的照片
  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  
  // 关闭上下文
  UIGraphicsEndImageContext();
  
  return newImage;
}

@end
